.TH hal_port "3hal" "2006-10-12" "LinuxCNC Documentation" "HAL"
.de FU
.sp
.ti -4
\\$*
..
.de FF
.br
.ti -4
\\$*
..
.SH NAME

hal_port \- a hal pin type that acts as an asyncronous one way byte stream

.SH SYNOPSIS

.FU .B #include <hal.h>
.FU bool hal_port_read(hal_port_t port, char* dest, unsigned count);
.FF bool hal_port_peek(hal_port_t port, char* dest, unsigned count);
.FF bool hal_port_peek_commit(hal_port_t port, unsigned count);
.FF unsigned hal_port_readable(hal_port_t port);
.FF void hal_port_clear(hal_port_t port);

.FU bool hal_port_write(hal_port_t port, const char* src, unsigned count);
.FF unsigned hal_port_writable(hal_port_t port);

.FU unsigned hal_port_buffer_size(hal_port_t port);

.FU .B #ifdef ULAPI
.FF void hal_port_wait_readable(hal_port_t **port, unsigned count, sig_atomic_t *stop);
.FF void hal_port_wait_writable(hal_port_t **port, unsigned count, sig_atomic_t *stop);
.FF .B #endif


.SH DESCRIPTION
A HAL port pin is a hal pin that acts as a one way byte oriented data stream in real time.
An output port on any component may be connected to an input port on any other component via a signal. Data written on the output pin becomes accessible to the input pin.
A HAL port signal may link only a single writer and a single reader.

A port also buffers data. Users should determine the proper buffer size based upon their intented application.


.SS \fBhal_port_read\fR
Reads count bytes from the port into destination buffer dest. hal_port_read will read \fIcount\fR bytes if and ony if \fIcount\fR bytes are available for reading and otherwise it will leave the port unaffected.
Returns true if count bytes were read and false otherwise.
This function should only be called by the component that owns the IN PORT pin.


.SS \fBhal_port_peek\fR
Behaves the same as hal_port_read, however it does not consume bytes from the hal port.
Repeated calls to hal_port_peek will return the same data.
Returns true if count bytes were read and false otherwise.
This function should only be called by the component that owns the IN PORT pin.


.SS \fBhal_port_peek_commit\fR
Advances the read position in the port buffer by count bytes. 
A hal_port_peek followed by a hal_port_peek_commit would function equivalently to hal_port_read given the same count value. 
Returns true if count readable bytes were skipped and are no longer accessible and false if no bytes wer skipped.
This function should only be called by the component that owns the IN PORT pin.


.SS \fBhal_port_readable\fR
Returns the number of bytes available for reading from port. It is safe to call this function from any component.

.SS \fBhal_port_clear\fR
Emptys a given port of all data.
hal_port_clear should only be called by the component that owns the IN PORT pin.

.SS \fBhal_port_write\fR
Writes count bytes from src into the port. 
Returns true if count bytes were written and false otherwise.
This function should only be called by the component that owns the OUT PORT pin.

.SS \fBhal_port_writable\fR
Returns the number of bytes that can be written into the port.
It is safe to call this function from any component.

.SS \fBhal_port_buffer_size\fR
Returns the maximum number of bytes that a port can buffer.
It is safe to call this function from any component.

.SS \fBhal_port_wait_readable\fR
Waits until the port has count bytes or more available for reading or the stop flag is set.

.SS \fBhal_port_wait_writable\fR
Waits until the port has count bytes or more available for writing or the stop flag is set.


.SH ARGUMENTS
.IP \fBhal_port_t\fR
A handle to a port object. Created by hal_pin_new

.IP \fBdest\fR
An array of bytes that hal_port_read and hal_port_peek will copy data into. This must be allocated by the caller and be at least count bytes long. 

.IP \fBcount\fR
The number of bytes that hal_port_read, hal_port_peek, and hal_port_write will copy in to dest or out from src

.IP \fBsrc\fR
An array of bytes that hal_port_write will copy data from into the port buffer. This must be of size count bytes or larger.

.IP \fBstop\fR
A pointer to a value which is monitored while waiting.  If it is nonzero,
the wait operation returns early.  This allows a wait call to be safely
terminated in the case of a signal.

.SH SAMPLE CODE
In the source tree under
 src/hal/components/raster.comp is a realtime component intended for laser control.
 src/tests/raster is a test program that also programs the raster component from python.

.SH REALTIME CONSIDERATIONS
.BR hal_port_read ", " hal_port_peek ", " hal_port_peek_commit ", " hal_port_readable ", " hal_port_clear ", " hal_port_write ", " hal_port_writable ", " hal_port_buffer_size
may be called from realtime code.

.BR hal_port_wait_writable ", " hal_port_wait_readable
may be called from ULAPI code.


.SH SEE ALSO
.BR raster (9),
